/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package xml;

import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.bind.Element;
import javax.xml.parsers.DocumentBuilder;
import no.met.metdata.proxy.*;
import org.w3c.dom.NodeList;

/**
 *
 * @author 490060
 */
public class WeatherFinder 
{
    private final float KELVIN = 273.15f;
    
    private ArrayList<Float> temperatures = null;
    private float currentTemp;
    private int iterator;
    private int currentHourGroup;
    
    public ArrayList<Float> getTemperatures()
    {
        return temperatures;
    }
    public WeatherFinder()
    {
        
    }
    
    public no_met_metdata_StationProperties getStation(String county, String name)
    {
        ArrayList<no_met_metdata_StationProperties> list = new ArrayList();
        MetServiceProxy mt = new MetServiceProxy();
        no_met_metdata_StationProperties[] station = null;
        try 
        {
            //station = mt.getStationsProperties(null, null);
            station = mt.getStationsFromTimeserieType("2", "");
 
            for(int i = 0; i < station.length; i++)
            {
               
                System.out.println("Station: " + station[i].getDepartment() + "\t" + station[i].getName());
                if(county.toUpperCase().equals(station[i].getDepartment()) 
                        && name.toUpperCase().equals(station[i].getName()))
                {
                    list.add(station[i]);
                    System.out.println(station[i].getStnr());
                }
            }
        } catch (Exception ex) 
        {
            Logger.getLogger(WeatherFinder.class.getName()).log(Level.SEVERE, null, ex);
        }
        return list.get(0);
    }
    
    public ArrayList<Float> getTemperatures(String start, String end, String county, String stationName, String hours)
    {//18700,50540". "0,6,12,18""2006-01-01"
        //if(temperatures != null)
        //{
            temperatures = new ArrayList<Float>();
            try {
                no_met_metdata_StationProperties station = getStation(county, stationName);
                String stationCode = station.getStnr() + "";
                String Startdate = station.getFromYear() + "-" 
                            + station.getFromMonth() + "-"
                            + station.getFromDay();
                String EndDate = station.getToYear() + "-"
                        + station.getToMonth() + "-"
                        + station.getToDay();
                System.out.println("STARTDATE: " + Startdate + "\nEND: " + EndDate);
                System.out.println(stationCode);
                MetServiceProxy mt = new MetServiceProxy();
                org.w3c.dom.Element data = mt.getMetDataValues("2", "", start, 
                end, stationCode, "tax,tan", hours , "", "");
                NodeList weatherElements = data.getElementsByTagName("weatherElement");
                for(int i = 0; i < weatherElements.getLength(); i += 2)
                {
                    float min = Float.parseFloat(weatherElements.item(i).getTextContent()) + 273.15f;
                    float max = Float.parseFloat(weatherElements.item(i + 1).getTextContent()) + 273.15f;
                    float temperature = (min + max) / 2 - KELVIN;
                    temperatures.add(temperature);
                }
            } catch (Exception ex) {
                Logger.getLogger(WeatherFinder.class.getName()).log(Level.SEVERE, null, ex);
            }
        //}
        
        return temperatures;
    }
   
    public float getSpecificTemperatures(String start, String end, String county, String stationName, String hours, String currentDate)
    {
        if(temperatures == null)
        {
            getTemperatures(start, end, county, stationName, hours);
            iterator = 0;
            currentTemp = temperatures.get(iterator); 
            currentHourGroup = 0;
        }
        int hour = Integer.parseInt(currentDate.split(",")[currentDate.split(",").length - 1]);
        int[] hourGroup = new int[hours.split(",").length];
        for(int i = 0; i < hourGroup.length; i++)
            hourGroup[i] = Integer.parseInt(hours.split(",")[i]);
        int tempHourGroup = currentHourGroup;
        
        for(int i = 1; i < hourGroup.length; i++)
        {
            if(hour < hourGroup[i] && hour >= hourGroup[i - 1])
                currentHourGroup = i - 1;
        }
        if(tempHourGroup != currentHourGroup)
            iterator++;
        currentTemp = temperatures.get(iterator);
            
        return currentTemp;
    }
    
    public static void main(String[] args)
    {
        try 
        {
            WeatherFinder wf = new WeatherFinder();
            /*no_met_metdata_StationProperties number = wf.getStation("TROMS", "TROMSØ");
            ArrayList test = wf.getTemperatures("2005-09-01", "2005-09-02", "TROMS", "TROMSØ", "0,6,12,18");
            for(int i = 0; i < test.size(); i++)
                System.out.println("Temp: " + test.get(i));*/
            //System.out.println(wf.getSpecificTemperatures("2005-09-01", "2005-09-05", "TROMS","TROMSØ", "0,6,12,18", "6,9,05,18") + "");
            System.out.println(wf.getStation("TROMS", "TROMSØ"));
            
        } catch (Exception ex) 
        {
            Logger.getLogger(WeatherFinder.class.getName()).log(Level.SEVERE, null, ex);
            ex.printStackTrace();
        }
    }
}
